import request from '@/utils/request.js'
// 上传meteorology数据接口的函数
export const meteorologyDataUploadService = (formData) => {
    return request.post('/meteorology/uploadData', formData, {
        headers: {
            "Content-Type": "multipart/form-data",
        },
    })
}
// 通过时间范围查找气象数据的函数
export const findMeteorologyByTimeRange = (startTime, endTime) => {
    // 将传入的日期时间字符串转换为Date对象
    const startDate = new Date(startTime);
    const endDate = new Date(endTime);

    // 如果需要确保格式为"yyyy-MM-dd HH:mm:ss"，可以使用之前讨论的方法之一进行格式化
    function formatDateToYMDHMS(date) {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }

    // 格式化开始时间和结束时间
    const startTimeFormatted = formatDateToYMDHMS(startDate);
    const endTimeFormatted = formatDateToYMDHMS(endDate);

    // 发送请求
    return request.get('/meteorology/findMeteorologyByTimeRange', {
        params: {
            startTime: startTimeFormatted,
            endTime: endTimeFormatted,
        }
    });
}
// 通过用户选择的时间查找气象数据的函数
export const findMeteorologyByUserSelectSingleTimeStamp = (userSelectSingleTimeStamp) => {   
        
    const formatDate = (date) => {
        const pad = (num) => num.toString().padStart(2, '0');
        const year = date.getFullYear();
        const month = pad(date.getMonth() + 1);
        const day = pad(date.getDate());
        const hours = pad(date.getHours());
        const minutes = pad(date.getMinutes());
        const seconds = pad(date.getSeconds());
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    };

    // 将用户选择的时间格式化为后端期望的格式
    const formattedTime = formatDate(new Date(userSelectSingleTimeStamp));

    // 发送请求
    return request.get('/meteorology/findMeteorologyByUserSelectSingleTimeStamp', {
        params: {
            singleTime: formattedTime // 使用后端接口期望的参数名称
        }
    });
}

// 默认查询数据接口,返回数据库中的时间节点最新的数据
export const findLatestMeteorology = () => {
    return request.get('/meteorology/findLatestMeteorology');
}

// 查询数据库中现有数据的最新日期的当天的全部数据
export const findMeteorologyInLatestDayData = () => {
    return request.get('/meteorology/findMeteorologyInLatestDayData');
}
